package com.net.shop.car.manager;

import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;

public class MapActivity extends BaseActivity implements OnClickListener, BaiduMap.OnMapClickListener, OnGetRoutePlanResultListener,
		OnGetGeoCoderResultListener, OnGetPoiSearchResultListener, OnGetSuggestionResultListener {
	private MapView mMapView;
	private BaiduMap mBaiduMap;
	// 定位
	private LocationClient mLocClient = null;
	private BDLocationListener myListener = new MyLocationListener();
	/** 纬度 */
	private double latitude;
	/** 经度 */
	private double longitude;
	/** 城市名 */
	private String cityName;
	// 搜索相关
	RoutePlanSearch mSearch = null; // 搜索模块，也可去掉地图模块独立使用
	// 初始化bitmap 信息，不用时及时 recycle
	private BitmapDescriptor bdA = BitmapDescriptorFactory.fromResource(R.drawable.icon_markmoren);
	private LocationMode mCurrentMode;
	BitmapDescriptor mCurrentMarker;
	private Button mBtnGoMyclocat;
	private Button mBtnMyGround;
	private static final int REQUEST_CODE = 1;
	private LatLng southwest;
	private PoiSearch mPoiSearch = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		// 定位
		mLocClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		mLocClient.registerLocationListener(myListener); // 注册监听函数
		setLocationOption();
		mLocClient.start();// 开始定位
		mBtnGoMyclocat = (Button) findViewById(R.id.go_myclocat);
		mBtnMyGround = (Button) findViewById(R.id.my_ground);
		mBtnGoMyclocat.setOnClickListener(this);
		mBtnMyGround.setOnClickListener(this);
	}

	@Override
	public void onGetSuggestionResult(SuggestionResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetPoiDetailResult(PoiDetailResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetPoiResult(PoiResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetDrivingRouteResult(DrivingRouteResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetTransitRouteResult(TransitRouteResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onGetWalkingRouteResult(WalkingRouteResult arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onMapClick(LatLng arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onMapPoiClick(MapPoi arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.go_myclocat:// 回原位置
			mCurrentMode = LocationMode.FOLLOWING;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
			mCurrentMode = LocationMode.NORMAL;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker));
			break;
		case R.id.my_ground:// 获取百度服务点类型
			Intent intent = new Intent();
			intent.setClass(MapActivity.this, MenuActivity.class);
			startActivityForResult(intent, REQUEST_CODE);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		switch (resultCode) {
		case 1:// 美食
			mBaiduMap.clear();
			String key = intent.getStringExtra("str_result");
			int juli;
			if ("信用社".equals(key) || "机动车检测".equals(key)) {
				juli = 50000;
			} else {
				juli = 10000;
			}
			if (latitude == 0 || longitude == 0) {
				Toast.makeText(MapActivity.this, "获取位置信息失败，请检查网络状态并重试！", Toast.LENGTH_LONG).show();
			} else {
				southwest = new LatLng(latitude, longitude);
				mPoiSearch.searchNearby(new PoiNearbySearchOption().keyword(key).location(southwest).radius(juli).pageCapacity(5000).pageNum(0));
			}

			break;

		default:
			break;
		}
	}

	/**
	 * 设置相关参数
	 */
	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(60000 * 5);// 设置发起定位请求的间隔时间为5min
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5); // 最多返回POI个数
		option.setPoiDistance(1000); // poi查询距离
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		mLocClient.setLocOption(option);
	}

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			cityName = location.getCity();
			saveToSharedPreferences(Constant.sp.city_name, cityName);
		}

		public void onReceivePoi(BDLocation poiLocation) {
			// 将在下个版本中去除poi功能
			if (poiLocation == null) {
				return;
			}
			latitude = poiLocation.getLatitude();
			longitude = poiLocation.getLongitude();
			cityName = poiLocation.getCity();
			saveToSharedPreferences(Constant.sp.city_name, cityName);
		}
	}

	// 定位到此结束
	/**
	 * 清所有Overlay
	 * 
	 * @param view
	 */
	public void clearOverlay(View view) {
		try {
			mBaiduMap.clear();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onPause() {
		// MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		mMapView.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		// MapView的生命周期与Activity同步，当activity恢复时需调用MapView.onResume()
		mMapView.onResume();

		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// MapView的生命周期与Activity同步，当activity�?毁时�?调用MapView.destroy()
		mSearch.destroy();
		mBaiduMap.setMyLocationEnabled(false);
		// �?出时�?毁定�?
		mLocClient.stop();
		mMapView.onDestroy();
		// 关闭定位图层
		super.onDestroy();
		// 回收 bitmap 资源
		bdA.recycle();
		// bdB.recycle();
		// bdC.recycle();
		// bdD.recycle();
		// bd.recycle();
		// bdGround.recycle();
		mLocClient.stop();// 停止定位
	}
}
